package com.leyou.cart.controller;

import com.leyou.cart.entity.Cart;
import com.leyou.cart.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分析购物车在redis中存储的数据结构，string， hash， list， set， zSet
 *  经过分析最终选择hash结构
 *  <key, <hashKey, Object>>
 *      key    ---userId
 *      hasKey ---skuId 快速找到对应sku，修改其中属性
 *      Object ---Sku购物车对象
 */
@RestController
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * 添加购物车
     */
    @PostMapping
    public ResponseEntity<Void> addCart(@RequestBody Cart cart){
        cartService.addCart(cart);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 查询购物车
     */
    @GetMapping("/list")
    public ResponseEntity<List<Cart>> findCarts(){
        List<Cart> carts = cartService.findCarts();
        return ResponseEntity.ok(carts);
    }

    /**
     * 批量添加购物车
     */
    @PostMapping("/list")
    public ResponseEntity<Void> addCarts(@RequestBody List<Cart> carts){
        cartService.addCarts(carts);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    /**
     * 商品加入收藏
     */
    @PostMapping("/attention")
    public ResponseEntity<Void>attention(@RequestBody Cart cart){
        cartService.attention(cart);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    @PostMapping("/collection")
    public ResponseEntity<Integer>collection(@RequestBody Cart cart){
        Integer f= cartService.collection(cart);
        return ResponseEntity.ok(f);
    }
    @GetMapping("/getAttention")
    public ResponseEntity<List<Cart>>getAttention(){
       List<Cart>skus= cartService.getAttention();
       return ResponseEntity.ok(skus);
    }
    /**
     * 删除购物车商品
     * @param skuId
     * @return
     */
    @DeleteMapping("/{skuId}")
    public ResponseEntity<Void>delCarts(@PathVariable("skuId") Long skuId){
        cartService.delCarts(skuId);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 修改购物车商品
     * @param id
     * @param num
     * @return
     */
    @PutMapping
    public ResponseEntity<Void>updataCart(@RequestParam("id") Long skuId,@RequestParam("num") Integer num){
        cartService.updataCart(skuId,num);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
    @PostMapping("/rmAttention")
    public ResponseEntity<Void>rmAttention(@RequestBody Cart cart){
        cartService.rmAttention(cart);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

}
